package com.hhsj.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import com.hhsj.ui.R;

import java.util.ArrayList;
import java.util.List;

//均分布局，设置布局的占比
public class EquallyLayoutV3 extends FrameLayout {

    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;
    private int splitNum = 2;
    private int orientation = HORIZONTAL;//水平
    private int spacing = 0;//间隙

    public EquallyLayoutV3(Context context) {
        super(context);
    }

    public EquallyLayoutV3(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public EquallyLayoutV3(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.EquallyLayout);
        if (array.hasValue(R.styleable.EquallyLayout_splitNum)) {
            splitNum = array.getInteger(R.styleable.EquallyLayout_splitNum, 1);
        }
        if (array.hasValue(R.styleable.EquallyLayout_android_orientation)) {
            orientation = array.getInt(R.styleable.EquallyLayout_android_orientation, HORIZONTAL);
        }

        if (array.hasValue(R.styleable.EquallyLayout_android_spacing)) {
            spacing = array.getDimensionPixelSize(R.styleable.EquallyLayout_android_spacing, 0);
        }
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);
        //更新子视图的layoutParams，
        int childCount = getChildCount();
        List<View> childs = new ArrayList<>();
        int usedSize = 0;//使用的size
        for (int i = 0; i < childCount; i++) {
            if (getChildAt(i).getVisibility() != GONE) {
                childs.add(getChildAt(i));
            }
        }
        for (int i = 0; i < childs.size(); i++) {
            MarginLayoutParams params = (MarginLayoutParams) childs.get(i).getLayoutParams();
            if (orientation == VERTICAL) {
                usedSize += params.topMargin;
                usedSize += params.bottomMargin;
            } else {
                usedSize += params.leftMargin;
                usedSize += params.rightMargin;
            }
            //间隙
            if (i < childs.size() - 1) {
                usedSize += spacing;
            }
        }


        if (childs.size() >= splitNum) {
            splitNum = childs.size();
        }
        int leftSize = Math.max(0, (orientation == VERTICAL ? getMeasuredHeight() - getPaddingTop() - getPaddingBottom() :
                getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) - usedSize);


        for (int i = 0; i < childs.size(); i++) {
            MarginLayoutParams params = (MarginLayoutParams) childs.get(i).getLayoutParams();

            int widthSpec;
            int heightSpec;
            int childWidth;
            int childHeight;

            if (orientation == VERTICAL) {
                params.height = leftSize / splitNum;
                childWidth = getMeasuredWidth() - (getPaddingLeft() + getPaddingRight() + params.leftMargin + params.rightMargin);
                childHeight = params.height;

                if (params.width == LayoutParams.MATCH_PARENT) {
                    widthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
                } else {
                    widthSpec = getChildMeasureSpec(widthMeasureSpec,
                            getPaddingLeft() + getPaddingRight() +
                                    params.leftMargin + params.rightMargin,
                            params.width);
                }
                heightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);

            } else {
                params.width = leftSize / splitNum;
                childWidth = params.width;
                childHeight = getMeasuredHeight() - (getPaddingTop() + getPaddingBottom() + params.topMargin + params.bottomMargin);

                widthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);

                if (params.height == LayoutParams.MATCH_PARENT) {
                    heightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
                } else {
                    heightSpec = getChildMeasureSpec(heightMeasureSpec,
                            getPaddingTop() + getPaddingBottom() +
                                    params.topMargin + params.bottomMargin,
                            params.height);
                }
            }
            childs.get(i).measure(widthSpec, heightSpec);
        }

    }


    @Override
    protected void onLayout(boolean force, int l, int t, int r, int b) {
        //更新子视图的layoutParams，

        int childCount = getChildCount();
        List<View> childs = new ArrayList<>();
        int usedSize = 0;
        for (int i = 0; i < childCount; i++) {
            if (getChildAt(i).getVisibility() != GONE) {
                childs.add(getChildAt(i));
            }
        }

        for (int i = 0; i < childs.size(); i++) {
            MarginLayoutParams params = (MarginLayoutParams) childs.get(i).getLayoutParams();
            if (orientation == VERTICAL) {
                usedSize += params.topMargin;
                getChildAt(i).layout(getPaddingLeft() + params.leftMargin,
                        getPaddingTop() + usedSize,
                        getPaddingLeft() + params.leftMargin + childs.get(i).getMeasuredWidth(),
                        getPaddingTop() + usedSize + params.height
                );
                usedSize += params.height;
                usedSize += params.bottomMargin;
            } else {
                usedSize += params.leftMargin;

                getChildAt(i).layout(getPaddingLeft() + usedSize,
                        getPaddingTop() + params.topMargin,
                        getPaddingLeft() + usedSize + params.width,
                        getPaddingTop() + params.topMargin + childs.get(i).getMeasuredHeight()
                );
                usedSize += params.width;
                usedSize += params.rightMargin;
            }
            usedSize += spacing;
        }


    }

    public void setSplitNum(int splitNum) {
        if (splitNum <= 1) {
            splitNum = 1;
        }
        this.splitNum = splitNum;
    }

    //方向
    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }

    //间隙
    public void setSpacing(int space) {
        this.spacing = space;
    }
}
